<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 03.08.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class clientNodeContactform
    ****
    * This classs represents a root page of any solution
    ****
    * @package clientNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 03.08.2005
    * @copyright essentialmind.com 2005
    ***/
    class clientNodeContactform extends clientNode{

        /**
         * Process inputs
         * 
         * @return void 
         **/
        public function process(){
            //disable caching
            $this->smarty->caching = false;
            
            //get default data
            $data = $this->db->getRow("SELECT * FROM node_contactform WHERE id = ".$this->object);
            $data['title'] = $this->db->getOne("SELECT title FROM pagemap WHERE id = ".$this->reference);
            
            //nothing
            $this->smarty->assign('data',$data);
            
            //process post if any
            if($_POST){
                //init error array
                $errors = array();
                
                //clean inputs
                $_POST['name']      = htmlspecialchars(trim(strip_tags($_POST['name'])));
                $_POST['desc']      = strip_tags(trim($_POST['desc']));
                $_POST['mail']      = htmlspecialchars(trim($_POST['mail']));
                
                //check that all field were filled out correctly
                if(empty($_POST['name'])){
                    $errors['name'] = true;   
                }
                
                if(empty($_POST['desc'])){
                    $errors['desc'] = true;   
                }
                
                if(empty($_POST['mail']) or !eregi('[-a-zA-Z0-9_\.]{1,}@[-a-zA-Z0-9_\.]{2,}\.[-a-zA-Z0-9_\.]{2,4}',$_POST['mail'])){
                    $errors['mail'] = true;   
                }
                
                //are there errors?
                if(empty($errors)){ //no
                    /*
                        Sending E-Mail to The Resposible Person
                        The basic idea is to prepare all the necessary data and then prefetch
                        a text based template by smarty. Then the output of this template
                        is passed to the e-mail gateway, which will send the e-mail to the end
                        user.
                    */
                    $this->smarty->assign(array(
                                                'cfName'    =>  $_POST['name'],
                                                'cfMail'    =>  $_POST['mail'],
                                                'cfText'    =>  $_POST['desc'],
                                                'cfTel'     =>  $_POST['tel']
                                                ));
                                                
                    $text = $this->smarty->preFetch('emails/contactform.txt');
					// $html = $this->smarty->preFetch('emails/contactform.html');
                    $myMailer = new mailGateway();
                    $myMailer->send($data['mailto'],'Contact Form Query',$text,$_POST['mail']);
                   
                   
                    //log action
                    $this->log->recordAction("Sent a Contact Form e-mail to " . $data['mailto']);
                    
					//put e-mail address into a cookie
                    setcookie('iMail',$_POST['mail'],time()+60*60*24*365,'/');
                    setcookie('iName',$_POST['name'],time()+60*60*24*365,'/');
                    
                    //redirect browser to confirmation page 
                    $goTo = $GLOBALS['_CONFIG']['server_root'];
                    if(!empty($_GET['data'])){
                        $goTo .= $_GET['data'].'/';
                    }
                    $goTo .= $_GET['page'] . '.html';
                    
                    header('Location: '.$goTo.'?confirm=true');
                    exit;
                }else{ //yes, there are errors
                    //tell smarty to report errors to the user   
                    $this->smarty->assign("error",$errors);
                } //if errors
            }else{
              //check if the client already submitted something in the past
              if(isset($_COOKIE['iMail'])){
                $_POST['mail'] = $_COOKIE['iMail'];
              }
              
              if(isset($_COOKIE['iName'])){
                $_POST['name'] = $_COOKIE['iName'];   
              }
            }//if post
        }
        

        /**
         * set the action template to page.html
         * 
         * @return void
         **/
        public function setTemplate(){
            $this->setActionTemplate('contactform.html');
        }
    }
?>